package com.code.test.first.hash;

import java.util.HashSet;
import java.util.Set;

/**
 * https://github.com/youngyangyang04/leetcode-master/blob/master/problems/0349.%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9A%84%E4%BA%A4%E9%9B%86.md
 * 题意：给定两个数组，编写一个函数来计算它们的交集。
 */
public class Code349 {

    public static void main(String[] args) {
        int[] nums1 = {2, 5, 6, 7, 8, 9};
        int[] nums2 = {2, 4, 6, 11, 24, 10};
        int[] nums3 = intersection(nums1, nums2);
        for (int i : nums3) {
            System.out.println(i);
        }
    }

    public static int[] intersection(int[] nums1, int[] nums2) {

        Set<Integer> s1 = new HashSet<>();
        Set<Integer> s2 = new HashSet<>();

        for (int i = 0; i < nums1.length; i++) {
            s1.add(nums1[i]);
        }

        for (int i = 0; i < nums2.length; i++) {
            if (s1.contains(nums2[i])) {
                s2.add(nums2[i]);
            }

        }

        return s2.stream().mapToInt(Integer::intValue).toArray();
    }


}
